/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168;

import Model.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.DataSource;
import javax.annotation.Resource;

public class ZpracovaniServlet extends HttpServlet {
    
    private MediumManagerImpl mediumManager = new MediumManagerImpl();
    
    @Resource(name="jdbc/MediumEvidence")
    void setDataSource(DataSource ds) {
        mediumManager.setDataSource(ds);
    }
    
    @Override
    public void init() throws ServletException {
        mediumManager.tryCreateTables();
    }
    
    private void listMediums (HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
         request.setAttribute("mediums", mediumManager.findAllMediums());
         request.getRequestDispatcher("seznam.jsp").forward(request, response);
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");    
        
        if (request.getServletPath().equals("/MediumList")) {
            listMediums(request, response);
        } else {
            /*String ref = request.getHeader("Referer");  /******** kontrola pristupu HIDDEN field / referer...
            PrintWriter out = response.getWriter();
            out.print(ref);
            return;*/
            
            String name = request.getParameter("name");
            String author = request.getParameter("author");
            String type = request.getParameter("type");
            String description = request.getParameter("description");

            if(name==null||name.length()==0
                    ||author==null||author.length()==0
                    ||type==null||type.length()==0
                    ||description==null||description.length()==0) {
                request.setAttribute("chyba", "Je nutné vyplnit všechny hodnoty !");
                request.setAttribute("name", name);
                request.setAttribute("author", author);
                request.setAttribute("type", type);
                request.setAttribute("description", description);
                request.getRequestDispatcher("/index.jsp").forward(request, response);
                return;
            }

            Medium medium = new Medium();
            medium.setName(name);
            medium.setAuthor(author);
            medium.setType(type);
            medium.setDescription(description);

            mediumManager.createMedium(medium);

            response.sendRedirect("MediumList");
            return;
        }
   }   
   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}